package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.ChartsSearchView;
import com.gable.report.model.User;
import com.gable.report.service.ChartsHospitalSearchService;
import com.gable.report.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gable.report.util.ChartsUtil.getHospitalId;

/**
 * 医院报表搜索
 * @author Aioria
 *
 */
@Controller
@RequestMapping("/hospital/search") /* 建议每个Controller都加上@RequestMapping采用Restful风格URL */
public class ChartsHospitalSearchController {
	
	private final Log logger = LogFactory.getLog(this.getClass());

	@Resource
	HttpServletRequest request;
	
	@Resource
	HttpServletResponse response;
	
	@Autowired
	ChartsHospitalSearchService chartsHospitalSearchService;

	@Autowired
	UserService userService;

	/**
	 * 科室列表
	 * @return
     */
	@RequestMapping(value="/department",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchDepartments(ChartsHospitalQuery chartsHospitalQuery){
		User user = (User)request.getSession().getAttribute("loginUser");
		Long hospitalId = getHospitalId(user,chartsHospitalQuery);
		if(hospitalId == null){
			return new ReturnResultUtil(0,"非法账户");
		}
		List<ChartsSearchView> list;
		try{
			list = chartsHospitalSearchService.queryDepartmentsList(hospitalId.toString());
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		Map map = new HashMap();
		map.put("list",list);
		return new ReturnResultUtil(1,"查询成功",map);
	}

	/**
	 * 设备类型列表
	 * @return
     */
	@RequestMapping(value="/type",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchDeviceTypes(ChartsHospitalQuery chartsHospitalQuery){
		User user = (User)request.getSession().getAttribute("loginUser");
		Long hospitalId = getHospitalId(user,chartsHospitalQuery);
		if(hospitalId == null){
			return new ReturnResultUtil(0,"非法账户");
		}
		List<ChartsSearchView> list;
		Map<String, List> map = new HashMap<>();
		try{
			list = chartsHospitalSearchService.queryFirstDeviceTypesList(hospitalId.toString());
			map.put("first",list);
			for (ChartsSearchView chartsSearchView : list) {
				List<ChartsSearchView> subList =  chartsHospitalSearchService.querySecondDeviceTypesList(hospitalId.toString(),chartsSearchView.getId());
				map.put(chartsSearchView.getId(),subList);
			}

		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}


	/** 
	 * @Author: 杨青桦
	 * @Description:  设备风险等级筛选
	 * @Date: 2017/8/14 17:13
	 * @return
     */
	@RequestMapping(value="/risk",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchDeviceRiskLevels(ChartsHospitalQuery chartsHospitalQuery){
		User user = (User)request.getSession().getAttribute("loginUser");
		Long hospitalId = getHospitalId(user,chartsHospitalQuery);
		if(hospitalId == null){
			return new ReturnResultUtil(0,"非法账户");
		}
		List<ChartsSearchView> list;
		Map<String, List> map = new HashMap<>();
		try{
			list = chartsHospitalSearchService.queryDeviceRiskLevels();
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}


	/** 
	 * @Author: 杨青桦
	 * @Description:  品牌列表
	 * @Date: 2017/8/14 17:10
	 * @return
     */
	@RequestMapping(value="/brand",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil queryDeviceAreaAndBrandList(ChartsHospitalQuery chartsHospitalQuery){
		User user = (User)request.getSession().getAttribute("loginUser");
		Long hospitalId = getHospitalId(user,chartsHospitalQuery);
		if(hospitalId == null){
			return new ReturnResultUtil(0,"非法账户");
		}
		Map<String, List> map = new HashMap<>();
		try{
			List<ChartsSearchView> list = chartsHospitalSearchService.queryDeviceBrandList(hospitalId.toString(),null);
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}


	/**
	 * @Author: 杨青桦
	 * @Description:  当前年份列表
	 * @Date: 2017/8/14 17:10
	 * @return
	 */
	@RequestMapping(value="/year",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchCurYears(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, List> map = new HashMap<>();
		try{
			List<ChartsSearchView> list = chartsHospitalSearchService.queryCurYears();
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}


	/**
	 * @Author: 杨青桦
	 * @Description:  报表截止日期
	 * @Date: 2017/8/18 13:26
	 * @return
     */
	@RequestMapping(value="/time",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil upToDate(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, String> map = new HashMap<>();
		try{
			Calendar a=Calendar.getInstance();
			int yearNum = a.get(Calendar.YEAR); //获取当前年份
			int monthNum = a.get(Calendar.MONTH)+1; //获取当前月份
			int dayNum = a.get(Calendar.DAY_OF_MONTH); //获取当前日期
			StringBuffer sb = new StringBuffer();
			map.put("time",sb.append(yearNum).append("年").append(monthNum).append("月").append(dayNum).append("日").toString());
			sb.setLength(0);
			map.put("enTime",sb.append(yearNum).append("-").append(monthNum).append("-").append(dayNum).toString());
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}


	/** 
	 * @Author: 杨青桦
	 * @Description:  设备默认金额区间
	 * @Date: 2017/9/20 15:47
	 * @return
	 */
	@RequestMapping(value="/price",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil SearchPriceCategory(ChartsHospitalQuery chartsHospitalQuery){
		User user = (User)request.getSession().getAttribute("loginUser");
		Long hospitalId = getHospitalId(user,chartsHospitalQuery);
		if(hospitalId == null){
			return new ReturnResultUtil(0,"非法账户");
		}
		Map<String, List> map = new HashMap<>();
		try{
			List<ChartsSearchView> list = chartsHospitalSearchService.queryPriceCategory(hospitalId.toString());
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}


	//下面的接口暂时用不到

	/**
	 * 维修商列表
	 * @return
     */
	@RequestMapping(value="/maintainer",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchMaintainers(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Long hospitalId = user.getHospitalId();
		List<ChartsSearchView> list = null;
		try{
			list = chartsHospitalSearchService.queryMaintainerList(hospitalId.toString());
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		Map map = new HashMap();
		map.put("list",list);
		return new ReturnResultUtil(1,"查询成功",map);
	}


	
}
